define('views/datalist', ['jquery'], function ($) {
    function DataList() {
        this.datalistContent = $('.data-list-content')
        this.datalistControlBtn = $('.show-data-list-btn')
    }

    DataList.prototype.Init = function () {
        var that = this
        $('.show-data-list-btn').on('click', function () {
            if (!that.datalistContent.hasClass('data-list-show')) {
                that.datalistContent.addClass('data-list-show')
                that.datalistControlBtn.text('返回地图')
            } else {
                that.datalistContent.removeClass('data-list-show')
                that.datalistControlBtn.text('查看列表')
            }
        })
        $('.data-list-content').on('click', 'tbody>tr', function (e) {
            var target = $(this)
            var lng = Number(target.attr('lng'))
            var lat = Number(target.attr('lat'))

            $('div.show-data-list-btn').click()

            var position = L.latLng(lat, lng)
            var zoom = window.map.getZoom()<13?13:window.map.getZoom()
            window.map.setView(position,zoom)

            var icon = L.icon({
                iconUrl: '/src/assets/img/toposition.gif',
                iconSize: [64, 64],
                iconAnchor: [32, 22],
            })
            var marker = L.marker(position,{icon:icon}).addTo(window.map)
            setTimeout(function(){
                marker.remove()
            },1800)
        })
    }


    DataList.prototype.ShowData = function (data) {
        var html = ''
        var newData = {}


        var templateHeader = '<table class="data-list-table" cellSpacing="0" cellPadding="0">'
            + '<thead>'
            + '<tr>'
            + '<th style="width: 54%;">曾确诊地</th>'
            + '<th style="width: 23%;">曾确诊人数</th>'
            + '<th style="width: 23%;">与我距离</th>'
            + '</tr>'
            + '</thead>'
            + '<tbody>'

        var templateFooter = '</tbody>' + ' </table>'

        var dressCount = data.length, peopleCount = 0;
        data.forEach(function (el) {
            if (el.district) {
                if (newData[el.district] == undefined) {
                    newData[el.district] = []
                }
                newData[el.district].push(el)
            }
            // 统计全市总人数
            if (Number(el.cntSumCertain) >= 0) {
                peopleCount += Number(el.cntSumCertain)
            }
        });

        // 显示全市总人数
        html += '<div class="data-list-table-title">'
            + $('#city').val() + '&nbsp;共<span style="color:red">'
            + dressCount + '个</span>曾确诊地&nbsp;<span>' + (peopleCount == 0 && dressCount > 0 ? '暂无上报信息' : ('<span style="color:red">' + peopleCount + '例</span>曾确诊病例')) + '</div>'


        for (var district in newData) {
            html += '<div class="data-list-table-title">' + district + '（<span style="color:red">' + newData[district].length + '个曾确诊地</span>）</div>'

            var tableHtml = templateHeader
            newData[district].forEach(function (item) {
                tableHtml += '<tr lng="' + item.lng + '" lat="' + item.lat + '"><td>' + item.showAddress + '</td>'
                    + '<td>' + (item.cntSumCertain >= 0 ? item.cntSumCertain : '暂无上报信息') + '</td>'
                    + '<td>' + (item.distance ? item.distance : '-') + '</td></tr>'
            })
            tableHtml += templateFooter
            html += tableHtml
        }
        // $('table.data-list-table>tbody').empty().html(html)
        $('.data-list-content').empty().html(html)
    }
    var datalist = new DataList()

    datalist.Init()

    return datalist
})